<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/layuimini/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="required|username" lay-reqtext="用户名不能为空" placeholder="请输入用户名"
                   value=""
                   class="layui-input">
            <tip>填写自己管理账号的名称</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">昵称</label>
        <div class="layui-input-block">
            <input type="text" name="nickname" lay-verify="required|nickname" lay-reqtext="昵称不能为空" placeholder="请输入昵称"
                   value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="true" title="男" checked="">
            <input type="radio" name="sex" value="false" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">手机</label>
        <div class="layui-input-block">
            <input type="number" name="phone" lay-verify="required|phone" lay-reqtext="手机不能为空" placeholder="请输入手机"
                   value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">邮箱</label>
        <div class="layui-input-block">
            <input type="email" name="email" lay-verify="required|email" placeholder="请输入邮箱" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="status" lay-skin="switch" lay-text="启用|停用" value="1" checked/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">角色</label>
        <div class="layui-input-inline">
            <select name="roleId" lay-search id="role-select" lay-verify="role" lay-reqtext="角色不能为空">
                <option value="" selected>请选择角色</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="/static/admin/layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //获取角色列表
        $.ajax({
            url: '/admin/role/list',
            type: 'GET',
            success: function (result) {
                if (result.code === 200) {
                    $.each(result.data, function () {
                        var roleId = $(this)[0].id;
                        var roleName = $(this)[0].roleName;
                        var categoryOption = $('<option></option>').val(roleId).text(roleName);
                        $('#role-select').append(categoryOption);
                    });
                    form.render('select');
                }
            }
        });

        //自定义验证规则
        form.verify({
            //验证用户名
            username: function (value) { //value：表单的值、item：表单的DOM对象
                if (value.length < 3 || value.length > 16) {
                    return '用户名长度为3-16个字符'
                }
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '用户名不能有特殊字符';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '用户名不能全为数字';
                }
            },
            //验证昵称
            nickname: function (value) { //value：表单的值、item：表单的DOM对象
                if (value.length < 3 || value.length > 16) {
                    return '昵称长度为3-16个字符'
                }
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '昵称不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '昵称首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '昵称不能全为数字';
                }
            },
            //验证角色
            role: function (value) {
                if (null == value || 0 === value.length) {
                    return '请为用户选择角色';
                }
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            axios({
                method: 'post',
                url: '/admin/user',
                data: data.field,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                },
            }).then((response) => {
                Swal.fire({
                    icon: 'success',
                    title: '保存成功',
                    showConfirmButton: false,
                    timer: 1500,
                    onClose: () => {
                        var parentLayer = parent.layer;
                        var iframeIndex = parentLayer.getFrameIndex(window.name);
                        parent.location.reload();
                        parentLayer.close(iframeIndex);
                    }
                });
            }).catch((error) => {
                Swal.fire({
                    icon: 'error',
                    title: '保存失败！',
                    text: `${error.response.data.message}`,
                });
            })
            return false;
        });

    });
</script>
</body>
</html>